package com.times.demo.common;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

/**
 * @ClassName Result
 * @Description TODO
 * @Author zyy
 * @Date 2024/2/27 15:38
 * @Version 1.0
 **/
@Data
public class Result<T> {

    /**
     * 状态码
     * 成功：200
     * 失败：其他
     */
    private int code;
    /**
     * 失败状态码描述
     * 如果成功不返回
     * 失败返回状态码对应的msg消息
     */
    private String msg;
    /**
     * 请求数据的结果
     */
    private T data;

    public Result() {
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> success(T t) {
        return new Result<T>(200, "success", t);
    }

    public static <T> Result<T> success() {
        return new Result<T>(200, "success");
    }

    public static <T> Result<T> fail(int code, String msg) {
        return new Result<T>(code, msg, null);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<T>(400, StrUtil.isBlank(msg) ? "fail" : msg, null);
    }

    public static <T> Result<T> fail() {
        return Result.fail(null);
    }
}
